package com.qiall.boot.util.controller.wather.init;

import com.alibaba.fastjson.JSON;
import com.qiall.boot.util.controller.wather.entity.GdCityInfoEntity;
import com.qiall.boot.util.controller.wather.entity.GdCityResponse;
import com.qiall.fileutil.EasyExcelUtil;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;

@Component
public class InitBaseInfo {
    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private GdCityResponse gdCityResponse;

    public void initGdCityCode() {
        elasticsearchTemplate.deleteIndex(GdCityInfoEntity.class);
        boolean index = elasticsearchTemplate.createIndex(GdCityInfoEntity.class);
        System.out.println(index);
        boolean putMapping = elasticsearchTemplate.putMapping(GdCityInfoEntity.class);

        System.out.println(putMapping);

    }

    public void initBaseData() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\zjutw\\Desktop\\1.xlsx");
        List<CityExcel> cityExcelList = EasyExcelUtil.readExcel(fileInputStream, CityExcel.class, new EasyExcelUtil.ExcelListener<>());
        for (int i = 0; i < cityExcelList.size(); i++) {
            GdCityInfoEntity cityInfoEntity = new GdCityInfoEntity();
            cityInfoEntity.setId(i+1);
            BeanUtils.copyProperties(cityExcelList.get(i), cityInfoEntity);
            gdCityResponse.save(cityInfoEntity);
        }

    }

    public void queryTest() {

        Iterable<GdCityInfoEntity> gdCityInfoEntities = gdCityResponse.findAll();
        Iterator<GdCityInfoEntity> gdCityInfoEntityIterator=gdCityInfoEntities.iterator();
        while (gdCityInfoEntityIterator.hasNext()){
            System.out.println(JSON.toJSONString(gdCityInfoEntityIterator.next()));
        }
        gdCityResponse.deleteAll();
    }


}
